/*
   Copyright (c) 2011, 2014 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-04 Marco Riccardi - initial implementation
 *  2014-12-20 Marco Riccardi - resource leak
 *
 */
package org.mizartools.system;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;

public class MessageFile {

	private static ArrayList<String> headerList = new ArrayList<String>();
	
	private static ArrayList<Error> errorList = new ArrayList<Error>();

	public static ArrayList<Error> getErrorList() {
		return errorList;
	}

	public static ArrayList<String> getHeaderList() {
		return headerList;
	}

	static {
		String mizFiles = EnvironFile.getMIZFILES();
		if (mizFiles != null){
			File file = new File(mizFiles, "mizar.msg");
			try {
				FileReader fileReader = new FileReader(file);
				try {
					LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
					String inputLine1;
					String inputLine2;
					String tmp;
					int iError;
					int index;
					// read header
					inputLine1 = lineNumberReader.readLine();
					headerList.add(inputLine1);
					inputLine1 = lineNumberReader.readLine();
					headerList.add(inputLine1);
					inputLine1 = lineNumberReader.readLine();
					headerList.add(inputLine1);
					
					while ((inputLine1 = lineNumberReader.readLine()) != null){
						inputLine2 = lineNumberReader.readLine();
						if (!inputLine1.equals("#")) {
							tmp = inputLine1.substring(2);
							index = tmp.indexOf(" ");
							if (index>0){tmp = tmp.substring(1, index);}
							iError = Integer.parseInt(tmp);
							errorList.add(new Error(iError, inputLine1, inputLine2));
						}
					}
					lineNumberReader.close();
				}
				catch (IOException e) {}
				finally{
					try {
						fileReader.close();
						} 
					catch (IOException e) {}
				}
			} catch (FileNotFoundException e) {}
		} 
	}
	
	private MessageFile(){
	}

}
